//!
//! Copyright (c) 2024-2999 北京心月狐科技有限公司 All rights reserved.
//!
//! https://www.mxxshop.com
//!
//! Licensed 并不是自由软件，未经许可不能去掉 MxxShop 相关版权
//!
//! 版权所有，侵权必究！
//!


use rbatis::rbdc::{DateTime, Decimal};
use serde::{Deserialize, Serialize};
use crate::modules::product::entity::cart_entity::Cart;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CartSaveRequest {
    /// 用户id
    pub user_id: Option<u64>,
    /// 商品id
    pub product_id: Option<u64>,
    /// 标题
    pub title: Option<String>,
    /// 封面图片
    pub images: Option<String>,
    /// 原价
    pub original_price: Option<Decimal>,
    /// 销售价格
    pub price: Option<Decimal>,
    /// 购买数量
    pub stock: Option<u32>,
    /// 规格
    pub spec: Option<String>,
}

impl From<CartSaveRequest> for Cart {
    fn from(request: CartSaveRequest) -> Self {
        Self {
            id: None,
            user_id: request.user_id,
            product_id: request.product_id,
            title: request.title,
            images: request.images,
            original_price: request.original_price,
            price: request.price,
            stock: request.stock,
            spec: request.spec,
            create_time: Option::from(DateTime::now()),
            update_time: Option::from(DateTime::now()),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CartUpdateRequest {
    /// 自增id
    pub id: Option<u64>,
    /// 用户id
    pub user_id: Option<u64>,
    /// 商品id
    pub product_id: Option<u64>,
    /// 标题
    pub title: Option<String>,
    /// 封面图片
    pub images: Option<String>,
    /// 原价
    pub original_price: Option<Decimal>,
    /// 销售价格
    pub price: Option<Decimal>,
    /// 购买数量
    pub stock: Option<u32>,
    /// 规格
    pub spec: Option<String>,
}

impl From<CartUpdateRequest> for Cart {
    fn from(request: CartUpdateRequest) -> Self {
        Self {
            id: request.id,
            user_id: request.user_id,
            product_id: request.product_id,
            title: request.title,
            images: request.images,
            original_price: request.original_price,
            price: request.price,
            stock: request.stock,
            spec: request.spec,
            create_time: None,
            update_time: Option::from(DateTime::now()),
        }
    }
}